<%--
  Created by IntelliJ IDEA.
  User: xiaomi
  Date: 2025/2/2
  Time: 19:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>

<head>
    <title>课程销售统计</title>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        h3 {
            text-align: center;
            margin: 10px 0;
        }
        #charts, #chart {
            display: flex;
            justify-content: space-around;
            width: 100%;
            max-width: 2000px;
            margin: 20px 0;
        }
        .context {
            border: 1px solid #ddd;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            margin: 0 10px;
            padding: 10px;
            border-radius: 5px;
            width: 100%;
        }
        canvas {
            width: 100%;
            height: 100%;
        }
    </style>
    <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', function () {
            // 初始化下拉框
            updateCharts();
            fetchMonthlySales();
        });

        function updateCharts() {
            // 获取圆形图数据
            $.ajax({
                url: './teacher/dailnum',
                type: 'POST',
                contentType: "application/json",
                success: function (data) {
                    // 初始化饼图
                    initPieChart(data);
                    // 初始化折线图
                    initLineChart(data);
                    // 初始化柱状图
                    initBarChart(data);
                },
                error: function () {
                    alert('圆形图数据获取失败');
                }
            });
            function initPieChart(data) {
                var pieData = [];
                var labels = [];
                var ctxPie = document.getElementById('pieChart').getContext('2d');
                for (var i = 0; i < data.data.length; i++) {
                    pieData.push(data.data[i].total_type);
                    labels.push(data.data[i].classtype);
                }
                new Chart(ctxPie, {
                    type: 'pie',
                    data: {
                        labels: labels,
                        datasets: [{
                            data: pieData,
                            backgroundColor: ['#FF6384', '#36A2EB']
                        }]
                    },
                    options: {
                        title: {
                            display: true,
                            text: '课程类型销售总数'
                        }
                    }
                });
            }
            function initLineChart(data){
                var lineData = [];
                var labels = [];
                var ctxLine = document.getElementById('lineChart').getContext('2d');

                for (var i = 0; i < data.data.length; i++) {
                    lineData.push(data.data[i].total_type);
                    labels.push(data.data[i].classtype);
                }
                new Chart(ctxLine, {
                    type: 'line',
                    data: {
                        labels: labels,
                        datasets: [{
                            label: '销售数量',
                            data: lineData,
                            borderColor: '#36A2EB',
                            fill: false
                        }]
                    },
                    options: {
                        scales: {
                            y: {
                                beginAtZero: true
                            }
                        }
                    }
                });
            }
           function initBarChart(data){
               var barData = [];
               var labels = [];
               var ctxBar = document.getElementById('barChart').getContext('2d');

               for (var i = 0; i < data.data.length; i++) {
                   barData.push(data.data[i].total_type);
                   labels.push(data.data[i].classtype);
               }
               new Chart(ctxBar, {
                   type: 'bar',
                   data: {
                       labels: labels,
                       datasets: [{
                           label: '销售数量',
                           data: barData,
                           backgroundColor: ['#FF6384', '#36A2EB']
                       }]
                   }
               });
           }
        }

        function fetchMonthlySales(){
            // 获取圆形图数据
            $.ajax({
                url: './teacher/dailmonth',
                type: 'POST',
                contentType: "application/json",
                success: function (data) {
                    // 初始化饼图
                    PieChart(data.data);
                    // 初始化折线图
                    LineChart(data.data);
                    // 初始化柱状图
                    BarChart(data.data);
                },
                error: function () {
                    alert('圆形图数据获取失败');
                }
            });
            function PieChart(data) {
                var pieData = [];
                var labels = [];
                var ctxPie = document.getElementById('pie').getContext('2d');
                var backgroundColors = ['#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0', '#9966FF']; // 可以根据需要扩展颜色

                data.forEach(function (item) {
                    pieData.push(item.total_type);
                    labels.push(item.classtype + " (" + item.month + ")");
                });

                new Chart(ctxPie, {
                    type: 'pie',
                    data: {
                        labels: labels,
                        datasets: [{
                            data: pieData,
                            backgroundColor: backgroundColors.slice(0, pieData.length)
                        }]
                    },
                    options: {
                        title: {
                            display: true,
                            text: '课程类型销售总数'
                        }
                    }
                });
            }
            function LineChart(data) {
                var lineData = [];
                var labels = [];
                var ctxLine = document.getElementById('line').getContext('2d');

                var datasets = {}; // 用于存储不同课程类型的销售数据

                data.forEach(function (item) {
                    if (!datasets[item.classtype]) {
                        datasets[item.classtype] = { label: item.classtype, data: [], borderColor: getRandomColor() };
                    }
                    datasets[item.classtype].data.push({ x: item.month, y: item.total_type });
                });

                // 将数据转换为 Chart.js 的格式
                var lineDatasets = Object.values(datasets);

                new Chart(ctxLine, {
                    type: 'line',
                    data: {
                        datasets: lineDatasets
                    },
                    options: {
                        scales: {
                            y: {
                                beginAtZero: true
                            }
                        }
                    }
                });
            }
            function BarChart(data) {
                var ctxBar = document.getElementById('bar').getContext('2d');
                var barData = [];
                var labels = [];
                var datasets = {}; // 用于存储不同课程类型的销售数据

                data.forEach(function (item) {
                    if (!datasets[item.classtype]) {
                        datasets[item.classtype] = { label: item.classtype, data: [], backgroundColor: getRandomColor() };
                    }
                    datasets[item.classtype].data.push({ x: item.month, y: item.total_type });
                });

                // 将数据转换为 Chart.js 的格式
                var barDatasets = Object.values(datasets);

                new Chart(ctxBar, {
                    type: 'bar',
                    data: {
                        datasets: barDatasets
                    },
                    options: {
                        scales: {
                            y: {
                                beginAtZero: true
                            }
                        }
                    }
                });
            }
            function getRandomColor() {
                var letters = '0123456789ABCDEF';
                var color = '#';
                for (var i = 0; i < 6; i++) {
                    color += letters[Math.floor(Math.random() * 16)];
                }
                return color;
            }
        }

    </script>
</head>

<body>
<div>
    <h3>课程总销量</h3>
<div id="charts">
    <div class="context">
    <canvas id="pieChart"  width="400" height="400"></canvas>
    </div>
    <div class="context">
    <canvas id="lineChart" width="400" height="400"></canvas>
    </div>
    <div class="context">
    <canvas id="barChart" width="400" height="400" ></canvas>
    </div>
</div>
</div>
<div>
    <h3>课程月销量</h3>
<div id="chart">
    <div class="context">
        <canvas id="pie"  width="400" height="400"></canvas>
    </div>
    <div class="context">
        <canvas id="line" width="400" height="400"></canvas>
    </div>
    <div class="context">
        <canvas id="bar" width="400" height="400" ></canvas>
    </div>
</div>
</div>
</body>
</html>